;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Corruption.ClueProcessing", [])
        .controller("ClueProcessingCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService","ybConfig","$rootScope", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService,ybConfig,$rootScope) {
            $scope.markInfo = {
                allChecked: false,
                curRow: -1
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.toolbarConfig = {
                showColConfig: false
            };


            $scope.condition = {};
            $scope.dataList = [];
            $scope.tbConf = {
                scrollWidth: 1070,
                tableCol: [
                    {
                        type: "",
                        name: "业务来源",
                        dataField: "source",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: "",
                        slot:false
                    },
                    {
                        type: "",
                        name: "业务表单名称",
                        dataField: "type",
                        width: 160,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: "",
                        slot:false
                    },
                    {
                        type: "",
                        name: "单号",
                        dataField: "billNo",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: "",
                        slot:false
                    },
                    {
                        type: "",
                        name: "处理状态",
                        dataField: "statusShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: "",
                        slot:false
                    },
                    {
                        type: "",
                        name: "核实情况",
                        dataField: "auditRemark",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: "",
                        slot:false
                    },
                    {
                        type: "",
                        name: "创建时间",
                        dataField: "createDateShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: "",
                        slot:false
                    },
                    {
                        type: "",
                        name: "预警查看",
                        dataField: "yujing",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: "",
                        slot:true
                    },  
                    {
                        type: "",
                        name: "督办单查看",
                        dataField: "duban",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: "",
                        slot: true
                    },
                    {
                        type: "",
                        name: "附件",
                        dataField: "down",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: "",
                        slot: true
                    },                 
                ]
            };

            $scope.exportParam = {
                sectionParam: {ids: []},
                allUrl: ybHttp.RequestUrl("device", "/report/exportDeliveryNoteWarn")
            };

            var btnPermissionCode = {
                clueProcessingUpload: window.ActionCode.clueProcessingUpload,
                clueProcessingEdit: window.ActionCode.clueProcessingEdit,
            };

            $scope.btnPermission = {
                clueProcessingUpload:true,
                clueProcessingEdit: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
                console.log(btnPermissionCode)
            }
            $scope.reset = function () {
                $scope.condition = {};
            };

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            function getDataList(curPage) {
                $scope.markInfo.allChecked = false;
                $scope.markInfo.curRow = -1;
                $scope.exportParam.sectionParam.ids = [];
                var service = "device";
                var reqUrl = "/clew/list";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    let status = {
                        "0":"待核实",
                        "1":"已核实"
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.createDateShow = $filter("YbDate")(item.createDate, "yyyy-MM-dd HH:mm:ss");
                        item.statusShow = status[item.status];
                        item.isChecked = false;
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.cueRow = index
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

			$scope.updateDbRemark = function(item) {
				handleUpdateDbRemark([item.id]);
			};
            function handleUpdateDbRemark(ids) {
				ybWidget.ShowConfirm("线索", '线索').then(function (val) {
					var service = "device";
					var reqUrl = "/clew/update";

                    var param = {
						ids: ids,
						auditRemark: val,
                    };
                    ybHttp.Post(service, reqUrl, param).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        getDataList();
                    })
                });
			}
            $scope.importAttach = function (item) {
                var param = {
                    name: "附件",
                    hideTemplate: true,
                    ignoreFormat: true,
                    otherParams:{
                        id: item.id,
                        // attach:item.attach,
                    },
                    importReqService: "device",
                    importReqUrl: "/clew/uploadFile",
                };
                ybWidget.ImportFile(param).then(function (data) {
                    getDataList(1);
                });
            };
            $scope.downloadAttach =  function downloadAttach(item) {
                window.open(ybConfig.getConfig().baseUrl.BjxlshFtp + item.attach) 
            }

            $scope.viewTo = function(type,item){
                console.log($rootScope.primaryMenuItem.children)
                let yujing = $rootScope.primaryMenuItem.children.filter(i => i.name == "在线监察", )[0]?.children
                let duban = $rootScope.primaryMenuItem.children.filter(i => i.name == "督察督办")[0]?.children
               // 业务来源:"source",业务表单名称:"type"
               let url = ""
               if (type == 'yujing') {
                let  yjArray = angular.copy(yujing)
                yjArray.forEach(i => {
                    const search = '预警'; 
                    const replaceWith = '督办单'; 
                     
                   i.name = i.name.split(search).join(replaceWith); 
                });
                 url = yjArray.filter(i => i.name == item.type)[0].state
               }else if (type == 'duban') {
                url = duban.filter(i => i.name == item.type)[0].state
               }
                // $state.go(obj[type],{"billNo": item.billNo});
                $state.go("app.corruption.dashboard", { fromState: url,billNo: item.billNo})
            };
            function getBillIds() {
                $scope.exportParam.sectionParam.ids = [];
                angular.forEach($scope.dataList, function (item) {
                    if (!!item.isChecked) {
                        $scope.exportParam.sectionParam.ids.push(item.id);
                    }
                });
            }

            $scope.selectAll = function (list, type) {
                for (var i = 0, len = list.length; i < len; i++) {
                    list[i].isChecked = angular.isUndefined(type) ? $scope.markInfo.allChecked : $scope.markInfo["allChecked" + type];
                }
                getBillIds();
            };

            function checkAll(list) {
                var selectAll = false;
                for (var i = 0, len = list.length; i < len; i++) {
                    if (!list[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            $scope.selectSingle = function (item, list, type) {
                var selectAll = false;
                if (!item.isChecked) {
                    selectAll = false;
                } else {
                    if (!checkAll(list)) {
                        selectAll = false;
                    } else {
                        selectAll = true;
                    }
                }
                if (angular.isDefined(type)) {
                    $scope.markInfo["allChecked" + type] = selectAll;
                } else {
                    $scope.markInfo.allChecked = selectAll;
                }
                getBillIds();
            };


            (function () {
                getDataList();
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])
    ;
})(window, window.angular);